package com.xiaoqi.lesson.config;

import com.xiaoqi.lesson.filter.CharsetFilter;
import com.xiaoqi.lesson.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import javax.servlet.FilterRegistration;

/**
 * @Author Rookie Coder
 * @Description 配置类
 * @Date
 **/
@Configuration
public class AllbeanConfig implements WebMvcConfigurer {

    @Value("${open.url}")
    private String openUrl;
   // 创建自定义过滤器
    @Bean
    public CharsetFilter getCharsetFilter(){
        return new CharsetFilter();
    }
    // 创建自定义拦截器
    @Bean
    public MyInterceptor getInterceptor(){
        return new MyInterceptor();
    }

    // 注入过滤器{这样做的目的是为了可以不使用注解注入到容器中，和方式一的结果是一样的}
    @Bean
    public FilterRegistrationBean getFilter(CharsetFilter charsetFilter){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(charsetFilter);
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.addUrlPatterns("/user/**");
        filterRegistrationBean.setName("myFilter");
        return filterRegistrationBean;
    }
    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getInterceptor()).addPathPatterns("/user/**").excludePathPatterns(openUrl);
    }



}
